// pages/my/my.js
var app = getApp()
Page({

    /**
     * Page initial data
     */
    data: {
        scrollTop: null,
        showServe: false,
        BASE_URL: app.globalData.BASE_URL,
        avatarUrl: "",
        nickName: "",
        tempFilePath: '',
		loadDone: false,
		all: 0,
		tixian: 0,
		shareNum: 0,
		phoneNumber: app.getmallhead().phoneNumber
    },
    /**
     * Lifecycle function--Called when page load
     */
    onLoad(options) {
		wx.hideHomeButton()
        if (!app.getopenid()) {
            wx.showToast({
                title: '登录已过期，请重新登录',
                duration: 1000,
                icon: 'none'
              })
            setTimeout(function() {
                wx.reLaunch({
                    url: '../index/index'
                })
              }, 1000)
        } else {
			this.getMyMoneyInfo()
        }
	},
	async getMyMoneyInfo() {
		this.data.loadDone = false
		wx.showLoading({
			title: '加载中',
		  })
        var url= app.globalData.BASE_URL + '/mallUserGetMyMoney'
        var data = {
            userId: app.getopenid(),
        }
        let ret = await app.reqServer(url, data, 'GET')
		console.log(ret)
		if (ret) {
			var tixianM = parseFloat(ret.balance)
			var allM = parseFloat(ret.total)
			if (tixianM > 0) {
				tixianM = tixianM/100.0
			}
			if (allM > 0) {
				allM = allM/100.0
			}
			this.setData(
				{
					avatarUrl: app.getnickavatar().avatarUrl,
					tempFilePath: "",
					nickName: app.getnickavatar().nickName,
					tixian: tixianM,
					all: allM,
					shareNum: ret.shareNum
				}
			)
			console.log(this.data)
			this.data.loadDone = true
			wx.hideLoading()
		}
    },

    /**
     * Lifecycle function--Called when page is initially rendered
     */
    onReady() {
    },

    /**
     * Lifecycle function--Called when page show
     */
    onShow() {
		wx.hideHomeButton()
    },

    /**
     * Lifecycle function--Called when page hide
     */
    onHide() {

    },

    /**
     * Lifecycle function--Called when page unload
     */
    onUnload() {
        this.data.loadDone = false
    },

    /**
     * Page event handler function--Called when user drop down
     */
    onPullDownRefresh() {

    },

    /**
     * Called when page reach bottom
     */
    onReachBottom() {

    },

    /**
     * Called when user click on the top right corner to share
     */
    onShareAppMessage() {

    },
    scroll: function (e) {
        this.setData({ scrollTop: e.detail.scrollTop })
    },
    onMineClick(e) {
        wx.redirectTo({
		  url: '../index/index',
		  fail(e) {
			console.log(e)
		  }
        })
    },
    onServeClick(e) {
        this.setData(
            {
                showServe: true
            }
        )
    },
    onPhoneClick() {
        wx.makePhoneCall({
          phoneNumber: this.data.phoneNumber,
        })
    },
    onServeClose(e) {
        this.setData(
            {
                showServe: false
            }
        )
    },
    onGetMyPost(e) {
        if (this.data.loadDone == false) {
            return
        }
        if (app.getoffline()) {
            wx.showToast({
              title: '活动已结束',
              icon: 'error'
            })
        } else {
			wx.redirectTo({
			  url: '../post/post',
			  fail(e) {
				  console.log(e)
			  }
			})
        }
    },
	onClickOrder(e) {
		wx.navigateTo({
		  url: '../my-order/my-order',
		  fail(res) {
			  console.log(res)
		  }
		})
	},
	onClickReward() {
		if (this.data.loadDone == false) {
			return
		}
		wx.redirectTo({
		  url: './tixian/tixian?maxNum=' + this.data.tixian,
		})
	},
	onClickProtocol(e) {
		wx.navigateTo({
		  url: './agreement/agreement',
		})
	}
})